package ui;

import java.io.*;
import tree.TreeCreator;
import tree.TreeCreatorImpl;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import parser.CmmLexer;
import parser.CmmParser;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import ir.*;

public class Main {
	public static void main(String[] args) throws  Exception{
		if(args.length==0) {
			TreeCreator treeCreator = new TreeCreatorImpl();
			EditorFrame editorFrame = new EditorFrame("CMM IDE", treeCreator);
			//editorFrame.setVisible(true);
			//editorFrame.initFrame();
			SpaceFrame spaceFrame = new SpaceFrame(editorFrame);
			spaceFrame.setVisible(true);
		} else if(args.length==2){
			switch (args[0]){
				case "-r":
					try {
						ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(args[1]));
						CmmLexer cl=new CmmLexer(input);
						CmmParser parser=new CmmParser(new CommonTokenStream(cl));
						ParseTree rule=parser.program();
						ParseTreeWalker walker=new ParseTreeWalker();
                        IRProduceListener IR = new IRProduceListener();
						walker.walk(IR,rule);
                        IRExecuter EX = new IRExecuter(IR.Code);
                        EX.excute();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					break;
				case "-c":
				case "-l":
				case "-s":
					break;
			}
		}
	}
}
